package cn.net.cfss.fgbp.provider.console.spi;

import cn.net.cfss.fgbp.api.console.constant.DeleteFlagEnum;
import cn.net.cfss.fgbp.api.console.dto.SysMenuDto;
import cn.net.cfss.fgbp.api.console.inte.ISysMenuService;
import cn.net.cfss.fgbp.api.console.vo.SysMenuVo;
import cn.net.cfss.fgbp.log.influxdb.ApiDto;
import cn.net.cfss.fgbp.log.statistics.LogDtoFactory;
import cn.net.cfss.fgbp.provider.console.biz.SysMenuDtoBiz;
import cn.net.cfss.fgbp.provider.console.utils.FgbpIdUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author oygq
 * @title: IMenuServiceImpl
 * @description: 处理有事务的操作
 * @date 2019/4/2 16:24
 */

@RestController
@Slf4j
public class SysMenuServiceSpi implements ISysMenuService {

    @Autowired
    SysMenuDtoBiz menuDtoBiz;

    /**
     * 菜单新增和修改
     *
     * @param menu
     */
    @Override
    public boolean insert(SysMenuVo menu) {
        try {
            menu.setId(FgbpIdUtil.generateId());
            menu.setCreateTime(new Date());
            menu.setDeleteFlag(DeleteFlagEnum.NOT_DEL);
            if(null == menu.getSort()){
                menu.setSort(0);
            }
            menuDtoBiz.insert(menu);
        } catch (Exception e) {
            log.error("菜单新增错误:{}", e.getMessage());
            return false;
        }
        log.info("请求参数:{}，返回参数：{}", menu, true);
        return true;
    }

    @Override
    public boolean update(SysMenuVo menu) {
        try {
            menu.setId(Long.valueOf(menu.getMenuId()));
            menu.setUpdateTime(new Date());
//            if (menu.getSort() == null) {
//                //如果不传sort，需要设置该值为空
//                menuDtoBiz.updateSortNullById(menu.getId());
//            }
            menuDtoBiz.updateSelectiveById(menu);
        } catch (Exception e) {
            log.error("菜单修改错误:{}", e.getMessage());
            return false;
        }
        log.info("请求参数:{}，返回参数：{}", menu, true);
        return true;
    }

    @Override
    public boolean delete(Long menuId) {
        SysMenuVo menu = new SysMenuVo();
        menu.setId(menuId);
        //逻辑删除
        menu.setDeleteFlag(DeleteFlagEnum.DEL);
        try {
            menuDtoBiz.updateSelectiveById(menu);
        } catch (Exception e) {
            log.error("菜单删除错误:{}", e.getMessage());
            return false;
        }
        log.info("请求参数:{}，返回参数：{}", menuId, true);

        return true;
    }

    @Override
    public boolean deleteBatch(List<Long> menuIds) {
        ApiDto apiDto = LogDtoFactory.getNewApiDto(UUID.randomUUID().toString(), "fgbp-provider-console", "menu/deleteBatch", null, "deleteBatch", "fgbp-cloud-console_0.0.1", null, null, null);

        try {
            menuDtoBiz.deleteBatch(menuIds);
        } catch (Exception e) {
            log.error("菜单批量删除错误:{}", e.getMessage());
            return false;
        }
        log.info("请求参数:{}，返回参数：{}", menuIds, true);
        return true;
    }

    @Override
    public SysMenuDto query(Long menuId) {

        /*SysMenuVo vo = new SysMenuVo();
        vo.setId(menuId);
        vo.setDeleteFlag(DeleteFlagEnum.NOT_DEL);
        SysMenuDto menu = menuDtoBiz.selectOne(vo);*/
        SysMenuDto menu = menuDtoBiz.selectById(menuId);
        //查询父对象
        if (null != menu && null != menu.getParentId()) {
            SysMenuVo parentVo = new SysMenuVo();
            parentVo.setId(menu.getParentId());
            parentVo.setDeleteFlag(DeleteFlagEnum.NOT_DEL);
            menu.setParent(menuDtoBiz.selectOne(parentVo));
        }

        log.info("请求参数:{}，返回参数：{}", menuId, menu);

        return menu;
    }

    @Override
    public List<SysMenuDto> queryList(SysMenuVo vo) {
        vo.setDeleteFlag(DeleteFlagEnum.NOT_DEL);
        List<SysMenuDto> dtoList = menuDtoBiz.queryList(vo);

        log.info("请求参数:{}，返回参数：{}", vo, dtoList);

        return dtoList;
    }

    @Override
    public List<SysMenuDto> tree(SysMenuVo vo) {
        List<SysMenuDto> dtoList = menuDtoBiz.tree(vo, true);
        log.info("请求参数:{}，返回参数：{}", vo, dtoList);
        return dtoList;
    }

    @Override
    public List<SysMenuDto> menuButtonTree(SysMenuVo vo) {
        List<SysMenuDto> dtoList = menuDtoBiz.tree(vo, false);

        log.info("请求参数:{}，返回参数：{}", vo, dtoList);
        return dtoList;

    }

    /**
     * 菜单唯一性校验
     *
     * @param permission
     * @return
     */
    @Override
    public boolean uniquenessCheck(String permission, Long id) {
        try {
            boolean flag = menuDtoBiz.check(permission, id);

            return flag;
        } catch (Exception e) {
            log.error("菜单唯一校验错误:{}", e.getMessage());

            return false;
        }
    }
}
